<?php
// retaille image
 	function resize($nomorig, $width){


	 	if (file_exists($nomorig))
 		{
       	 $extension = substr($nomorig, strlen($nomorig)-3);

  		 // chargement img origine
  		 if(strtolower($extension) == "gif"){
   		     $image_orig = imagecreatefromgif($nomorig);
  			  // Cacul des nouvelles dimensions
    	     list($width_orig, $height_orig) = getimagesize($nomorig);

      	    if($width_orig>$width)
       	       $height = ($width * $height_orig) / $width_orig;
          else {
                   $height = ceil($height_orig);
                   $width = ceil($width_orig);
          }


        $image_new = imagecreatetruecolor($width, $height);

        $src_tc = imagecolorsforindex($image_orig, $src_tc_idx);
        $tgt_tc_idx = imagecolorallocate($image_new, $src_tc['red'], $src_tc['green'], $src_tc['blue']);
        imagefill($image_new, 0, 0, $tgt_tc_idx);
        imagecolortransparent($image_new, $tgt_tc_idx);

   }
   else if(strtolower($extension) == "jpg"){
        $image_orig = imagecreatefromjpeg($nomorig);
    // Cacul des nouvelles dimensions
      list($width_orig, $height_orig) = getimagesize($nomorig);
        if($width_orig>$width)
            $height = ($width * $height_orig) / $width_orig;
         else {
                $height = ceil($height_orig);
                $width = ceil($width_orig);
          }

           $image_new = imagecreatetruecolor($width, $height);

   }

   else if(strtolower($extension) == "png"){
        $image_orig = imagecreatefrompng($nomorig);
    // Cacul des nouvelles dimensions
      list($width_orig, $height_orig) = getimagesize($nomorig);
        if($width_orig>$width)
            $height = ($width * $height_orig) / $width_orig;
         else {
                $height = ceil($height_orig);
                $width = ceil($width_orig);
          }

           $image_new = imagecreatetruecolor($width, $height);

   }
  // Redimensionnement

  imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);


     if(strtolower($extension) == "gif")
                 imagegif($image_new, "$nomorig");
        else if(strtolower($extension) == "jpg")
                 imagejpeg($image_new, "$nomorig", 75);
        else if(strtolower($extension) == "png")
                 imagepng($image_new, "$nomorig", 7);

  		}

 	}

	// function de retournement d'image
	function imageflip(&$dest, &$src) {
        $w = imagesx($src);
        $h = imagesy($src);
        $alpha = 127;
        for($y=0; $y<$h; $y++) {
                for ($x=0; $x<$w; $x++) {
                        $couleur = imagecolorsforindex($src, imagecolorat($src, $x, $y));
                        $couleurAlpha = imagecolorallocatealpha($dest, $couleur['red'], $couleur['green'], $couleur['blue'], $alpha);
                        imagesetpixel($dest, $x, ($h-$y), $couleurAlpha);
                }
                if($alpha > 1) $alpha--;
        }
	}


	function redim($type, $nomorig, $width="", $height="", $opacite="", $nb="", $miroir="", $checktype=1){

		if($checktype == 1 && $type != "produit" && $type !="rubrique" && $type != "contenu" && $type != "dossier")
			return "";

		$nomorig = realpath(dirname(__FILE__)) . "/../client/gfx/photos/$type/" . $nomorig;

 		if (file_exists($nomorig) && preg_match("/([^\/]*).((jpg|gif|png|jpeg))/i", $nomorig, $nsimple)) {

 			$extension = strtolower($nsimple[2]);

  			$nomcache = "client/cache/" . $type . "/" . $width . "_" . $height . "_" . $opacite . "_" . $nb . "_" . $miroir . "_" . $nsimple[1] . "." . $nsimple[2];

 			if(file_exists(realpath(dirname(__FILE__)) . "/../" . "$nomcache"))
				return $nomcache;

  	   		// Cacul des nouvelles dimensions
	      	list($width_orig, $height_orig) = getimagesize($nomorig);

			 // si l'image est plus grande
			$image_p_largeur = "";
			$image_p_hauteur = "";

			if ( ($width_orig > $width) || ($height_orig > $height) )
			{
				if (($width_orig > $width) && $width!="")
				{
					# Calcul 1 : la largeur
					$facteur_div = $width_orig / $width ;

					$image_p_largeur = $width ; /* Nouvelle largeur */
					$image_p_hauteur = $height_orig / $facteur_div ; /* Nouvelle hauteur */
				}
				else {
					$image_p_largeur = $width_orig ; /* Nouvelle largeur */
					$image_p_hauteur = $height_orig ; /* Nouvelle hauteur */
				}

				if (($image_p_hauteur > $height) && $height!="")
				{
					# Calcul 2 : la hauteur
					$facteur_div = $image_p_hauteur / $height ;

					$image_p_largeur = $image_p_largeur / $facteur_div ; /* Nouvelle largeur */
					$image_p_hauteur = $height ; /* Nouvelle hauteur */
				}
				$width = $image_p_largeur ;
				$height = $image_p_hauteur ;
			}

			else
			{
				$width = "";
				$height = "";
			}

			if($width=="") $width=$width_orig;
			if($height=="") $height=$height_orig;

	        $image_new = imagecreatetruecolor($width, $height);

   			// chargement img origine
   			if($extension == "gif"){
        		$image_orig = imagecreatefromgif($nomorig);
   			}
   			else if($extension == "jpg" || $extension == "jpeg" || $extension == "png") {

	        	if($extension == "jpg" || $extension == "jpeg")
					$image_orig = imagecreatefromjpeg($nomorig);
				else if($extension == "png") {
					$image_orig = imagecreatefrompng($nomorig);

            	$trnprt_indx = imagecolortransparent($image_orig);

            	// If we have a specific transparent color
            	if ($trnprt_indx >= 0) {

	                // Get the original image's transparent color's RGB values
	                $trnprt_color = imagecolorsforindex($image_orig, $trnprt_indx);

	                // Allocate the same color in the new image resource
	                $trnprt_indx = imagecolorallocate($image_new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

	                // Completely fill the background of the new image with allocated color.
	                imagefill($image_new, 0, 0, $trnprt_indx);

	                // Set the background color for new image to transparent
	                imagecolortransparent($image_new, $trnprt_indx);
            	}
				else {

             		// Turn off transparency blending (temporarily)
                	imagealphablending($image_new, false);

                	// Create a new transparent color for image
                	$color = imagecolorallocatealpha($image_new, 0, 0, 0, 127);

                	// Completely fill the background of the new image with allocated color.
                	imagefill($image_new, 0, 0, $color);

                	// Restore transparency blending
                	imagesavealpha($image_new, true);
				}
		 	}
		}


		if($opacite != ""){
			$opac_img = imagecreatetruecolor($width_orig, $height_orig);
			$white = ImageColorAllocate ($opac_img, 255, 255, 255);

			imagefill($opac_img, 0, 0, $white);

			imagecopymerge($opac_img, $image_orig, 0,0,0,0, $width_orig, $height_orig, $opacite);

        	$image_orig = $opac_img;
		}

  		// Redimensionnement
  		imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
		// Noir et blanc

		if($nb != ""){

  			imagetruecolortopalette($image_new, false, 256);

 			$total = ImageColorsTotal($image_new);

			for( $i=0; $i<$total; $i++){
	 	    	 $old = ImageColorsForIndex($image_new, $i);
 	 	    	 $commongrey = (int)(($old[red] + $old[green] + $old[blue]) / 3);
 	 	     	ImageColorSet($image_new, $i, $commongrey, $commongrey, $commongrey);
 	    	}
		}

		if($miroir != ""){

			$largeur = imagesx($image_new);
			$hauteur = imagesy($image_new);

			$temporaireUn = imagecreatetruecolor($largeur, 50);
			$temporaireDeux = imagecreatetruecolor($largeur, 50);
			$resultat = imagecreatetruecolor($largeur, $hauteur+50);

			$blancUn = imagecolorallocate($resultat, 255, 255, 255);
			imagefill($resultat, 1, 1, $blancUn);
			$blancDeux = imagecolorallocate($temporaireDeux, 255, 255, 255);
			imagefill($temporaireDeux, 1, 1, $blancDeux);

			imagecopy ($resultat, $image_new, 0, 0, 0, 0, $largeur, $hauteur);
			imagecopy ($temporaireUn, $image_new, 0, 0, 0, $hauteur-50, $largeur, 50);
			imageflip($temporaireDeux, $temporaireUn);
			imagecopy ($resultat, $temporaireDeux, 0, $hauteur, 0, 0, $largeur, 50);
			$image_new = $resultat;
		}


     	if($extension == "gif"){
     		imagegif($image_new, realpath(dirname(__FILE__)) . "/../" . $nomcache);
     	}
     	else if($extension == "jpg" || $extension == "jpeg"){
			imagejpeg($image_new, realpath(dirname(__FILE__)) . "/../" . $nomcache, 75);
     	}
  	    else if($extension == "png"){
			imagepng($image_new, realpath(dirname(__FILE__)) . "/../" . $nomcache, 7);
      	}
     }

	return $nomcache;
  }


?>
